#include "editsignalreply.h"
#include "qsignalslotcenter.h"
#include "simpleeditorcommand.h"
#include "uicommandmanager.h"
#include "pickmanager.h"
#include <QStringList>
#include "layersmanager.h"

const float oncerotavalue = 45;

namespace PublicGui
{
	EditSignalReply::EditSignalReply(QObject* parent,CoreBase::Viewer* viewer,CoreBase::Scene* scene):QObject(parent)
	{
		//connect(&QSignalSlotCenter::QSignalSlotCenter::GetInstance(),SIGNAL(signalCurrentMapChanged()),this,SLOT(slotRefreshTree()));
		//connect(&QSignalSlotCenter::QSignalSlotCenter::GetInstance(),SIGNAL(signalClearAll()),this,SLOT(slotClearScene()));
		connect(&QSignalSlotCenter::QSignalSlotCenter::GetInstance(),SIGNAL(signalCreateDevices(
			QVector<osg::ref_ptr<CoreBase::ActorBase>>,
			QVector<osg::ref_ptr<CoreBase::ActorBase>>)),this,SLOT(slotsCreateDevices(
			QVector<osg::ref_ptr<CoreBase::ActorBase>>,
			QVector<osg::ref_ptr<CoreBase::ActorBase>>)));
		connect(&QSignalSlotCenter::QSignalSlotCenter::GetInstance(),SIGNAL(signalActorPropertyChanged(
			QVector<CoreBase::ActorBase*>,QVector<QString>,QVector<QString>,QString)),this,
			SLOT(slotsActorPropertyChanged(QVector<CoreBase::ActorBase*>,QVector<QString>,QVector<QString>,QString)));
		connect(&QSignalSlotCenter::QSignalSlotCenter::GetInstance(),SIGNAL(signalActionTrigger(int)),this,
			SLOT(slotMainActionTrigger(int)));
	}

	EditSignalReply::~EditSignalReply()
	{}

	void EditSignalReply::slotsCreateDevices(QVector<osg::ref_ptr<CoreBase::ActorBase>> vector,
			QVector<osg::ref_ptr<CoreBase::ActorBase>> targetvector)
	{
		QSignalSlotCenter::CreateDeleteActorCommand* command = new QSignalSlotCenter::CreateDeleteActorCommand(ACTION_CREAT_DEVICE);
		osg::ref_ptr<QSignalSlotCenter::CreateDeleteActorData> data = new QSignalSlotCenter::CreateDeleteActorData;
		data->_objectVector = vector;
		if(targetvector.size()<1)
			targetvector.push_back(VrDataCore::LayersManager::GetInstance().getStaticLayer());
		data->_targetActorVector = targetvector;
		command->setCreateActorData(data);
		QSignalSlotCenter::UICommandManager::GetInstance().runCommand(command);
	}

	void EditSignalReply::slotsActorPropertyChanged(QVector<CoreBase::ActorBase*> actor,QVector<QString> oldval,
			QVector<QString> newval,QString propertyname)
	{
		QSignalSlotCenter::PropertyEditorCommand* command = new QSignalSlotCenter::PropertyEditorCommand(ACTION_NO_DEFINE);
		osg::ref_ptr<QSignalSlotCenter::PropertyTypeData> data = new QSignalSlotCenter::PropertyTypeData(actor,oldval
			,newval,propertyname);
		command->setPropertyTypeDataObject(data);
		QSignalSlotCenter::UICommandManager::GetInstance().runCommand(command);
	}

	void EditSignalReply::slotMainActionTrigger(int action)
	{
		switch(action)
		{
		case ACTION_DELETE_DEVICE:
			doDeleteObject();
			break;
		case ACTION_ROTATE_OBJECT:
			doRotateObject();
			break;
		default:
			break;
		}
	}

	void EditSignalReply::doDeleteObject()
	{
		QVector<osg::ref_ptr<CoreBase::ActorBase>> vector;
		QVector<osg::ref_ptr<CoreBase::ActorBase>> targetvector;
		QVector<CoreBase::Drawable*> preselectobjects =VrDataCore::PickManager::GetInstance().getPickObjects();
		for(int i=0;i<preselectobjects.size();++i)
		{
			vector.push_back(osg::ref_ptr<CoreBase::ActorBase>(preselectobjects.at(i)->getOwnerActor()));
			if(preselectobjects.at(i)->getParent()!=NULL)
			{
				osg::ref_ptr<CoreBase::ActorBase> parent = preselectobjects.at(i)->getParent()->getOwnerActor();
				targetvector.push_back(parent);
			}
			else
				targetvector.push_back(VrDataCore::LayersManager::GetInstance().getStaticLayer());
		}
		QSignalSlotCenter::CreateDeleteActorCommand* command = new QSignalSlotCenter::CreateDeleteActorCommand(ACTION_DELETE_DEVICE);
		osg::ref_ptr<QSignalSlotCenter::CreateDeleteActorData> data = new QSignalSlotCenter::CreateDeleteActorData;
		data->_objectVector = vector;
		data->_targetActorVector = targetvector;
		command->setCreateActorData(data);
		QSignalSlotCenter::UICommandManager::GetInstance().runCommand(command);
	}

	void EditSignalReply::doRotateObject()
	{
		QVector<CoreBase::ActorBase*> actorvector;
		QVector<QString> oldrotavalvector;
		QVector<QString> newrotavalvector;
		QVector<CoreBase::Drawable*> preselectobjects =VrDataCore::PickManager::GetInstance().getPickObjects();
		for(int i=0;i<preselectobjects.size();++i)
		{
			CoreBase::ActorBase* actor = preselectobjects.at(i)->getOwnerActor();
			actorvector.push_back(osg::ref_ptr<CoreBase::ActorBase>(actor));
			oldrotavalvector.push_back(actor->getProperty(ACTOR_PROPERYT_ROTATION)->toQString());
		}
		for(int i=0;i<oldrotavalvector.size();++i)
		{
			QStringList vallist = oldrotavalvector.at(i).split(' ',QString::SkipEmptyParts);
			QString newstr = vallist.at(0)+" "+vallist.at(1)+" "+QString::number(vallist.at(2).toFloat()+oncerotavalue);
			newrotavalvector.push_back(newstr);
		}
		QSignalSlotCenter::PropertyEditorCommand* command = new QSignalSlotCenter::PropertyEditorCommand(ACTION_ROTATE_OBJECT);
		osg::ref_ptr<QSignalSlotCenter::PropertyTypeData> data = new QSignalSlotCenter::PropertyTypeData(actorvector,oldrotavalvector
			,newrotavalvector,ACTOR_PROPERYT_ROTATION);
		command->setPropertyTypeDataObject(data);
		QSignalSlotCenter::UICommandManager::GetInstance().runCommand(command);
	}
}